/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.util.Date;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
import test.model.RegisterForm;
import test.services.RegService;

/**
 *
 * @author hjhi
 */
@Controller
@RequestMapping("register.htm")
public class RegistrationController {

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        RegisterForm regForm = new RegisterForm();
        model.put("regForm", regForm);
        return "regform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid RegisterForm regForm, BindingResult result,
            Map model) {

        RegService rs = new RegService();

        String companyName = regForm.getCompanyName();
        String owner = regForm.getOwner();
        Date regDate = regForm.getRegDate();
        String location = regForm.getLocation();

        if (result.hasErrors()) {
            return "redirect";
        }

        int num = rs.registerUser(companyName, owner, regDate, location);
        regForm = (RegisterForm) model.get("regForm");
        if (num != 1) {
            return "redirect";
        }
//        model.put("regForm", regForm);
        return "success";
    }
}
